﻿namespace WMS6.Web
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    using System.Web.Security;

    public class StrUtils
    {
        private static byte[] Keys = new byte[] { 9, 0x87, 0x65, 0x43, 0x21, 0xab, 0xcd, 0xef };

        public static bool CompByteArray(byte[] bOld, byte[] bNew)
        {
            if ((bOld != null) && (bNew != null))
            {
                if (bOld.Length == bNew.Length)
                {
                    for (int i = 0; i < bOld.Length; i++)
                    {
                        if (bOld[i] != bNew[i])
                        {
                            return false;
                        }
                    }
                    return true;
                }
                return false;
            }
            return false;
        }

        public static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }

        public static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }

        public static DateTime FormatDate(DateTime date)
        {
            return DateTime.Parse(date.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo));
        }

        public static string FormatNull(object obj)
        {
            if (obj == null)
            {
                return "";
            }
            return obj.ToString().Trim();
        }

        public static string FormatSQLBool(bool iParam)
        {
            if (iParam)
            {
                return "1";
            }
            return "0";
        }

        public static string FormatSQLDate(DateTime date)
        {
            if (("'" + date.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo) + "'") == "'0001-01-01'")
            {
                return "NULL";
            }
            return ("'" + date.ToString() + "'");
        }

        public static string FormatSQLDouble(decimal iParam)
        {
            return iParam.ToString().Trim();
        }

        public static string FormatSQLDouble(double iParam)
        {
            return iParam.ToString().Trim();
        }

        public static string FormatSQLInt(int iParam)
        {
            return iParam.ToString().Trim();
        }

        public static string FormatSQLStr(string sParam)
        {
            string sTemp = sParam;
            if (sParam == null)
            {
                return "''";
            }
            sTemp = sParam.Replace("'", "").Replace("\"", "").Trim();
            return ("'" + sTemp + "'");
        }

        public static List<string> GetStrBySplitStr(string sStr, string sSplit)
        {
            string[] ss = sStr.Split(new string[] { sSplit }, StringSplitOptions.None);
            if ((ss != null) && (ss.Length > 0))
            {
                List<string> ls = new List<string>();
                for (int i = 0; i < ss.Length; i++)
                {
                    ls.Add(ss[i]);
                }
                return ls;
            }
            return null;
        }

        public static string InputText(string inputString, int maxLength)
        {
            StringBuilder retVal = new StringBuilder();
            if ((inputString != null) && (inputString != string.Empty))
            {
                inputString = inputString.Trim();
                if (inputString.Length > maxLength)
                {
                    inputString = inputString.Substring(0, maxLength);
                }
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        case '<':
                        {
                            retVal.Append("&lt;");
                            continue;
                        }
                        case '>':
                        {
                            retVal.Append("&gt;");
                            continue;
                        }
                        case '"':
                        {
                            retVal.Append("&quot;");
                            continue;
                        }
                    }
                    retVal.Append(inputString[i]);
                }
                retVal.Replace("'", " ");
            }
            return retVal.ToString();
        }

        public static string MD5EncryptString(string str, int code)
        {
            if (code == 0x10)
            {
                return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 0x10);
            }
            return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();
        }

        public static string SaltDecryptString(string str)
        {
            string passParase = "qtEntryptConst";
            string initVector = "1a2B3C4D5e6F7G8H";
            RijndaelEnhanced key = new RijndaelEnhanced(passParase, initVector);
            return key.Decrypt(str);
        }

        public static string SaltEncryptString(string str)
        {
            string passParase = "qtEntryptConst";
            string initVector = "1a2B3C4D5e6F7G8H";
            RijndaelEnhanced key = new RijndaelEnhanced(passParase, initVector);
            return key.Encrypt(str);
        }

        public static string SplitParam(string sParam)
        {
            string sResult = "(";
            string[] inParam = sParam.Split(",".ToCharArray());
            foreach (string s in inParam)
            {
                sResult = sResult + "'" + s.Trim() + "',";
            }
            return (sResult.Substring(0, sResult.Length - 1) + ")");
        }
    }
}

